package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.entity.regi.CqCarModelEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.excel.annotation.ResponseExcel;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.regi.service.CqCarModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车型管理
 *
 * @author lucc
 * @date 2023-10-17 11:43:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqCarModel")
@Tag(description = "cqCarModel", name = "车型管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqCarModelController {
    private final CqCarModelService cqCarModelService;

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param cqCarModel 车型管理
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    // @PreAuthorize("@pms.hasPermission('eam_cqCarModel_view')" )
    public R getCqCarModelPage(@ParameterObject Page page, @ParameterObject CqCarModelEntity cqCarModel) {
        LambdaQueryWrapper<CqCarModelEntity> wrapper = Wrappers.lambdaQuery(cqCarModel).orderByDesc(CqCarModelEntity::getCreateTime);
        return R.ok(cqCarModelService.page(page, wrapper));
    }

    /**
     * 通过id查询车型管理
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(cqCarModelService.getById(id));
    }

    /**
     * 新增车型管理
     *
     * @param cqCarModel 车型管理
     * @return R
     */
    // @Operation(summary = "新增车型管理" , description = "新增车型管理" )
    @SysLog("新增车型管理")
    @PostMapping
    public R save(@RequestBody CqCarModelEntity cqCarModel) {
        return R.ok(cqCarModelService.save(cqCarModel));
    }

    /**
     * 修改车型管理
     *
     * @param cqCarModel 车型管理
     * @return R
     */
    // @Operation(summary = "修改车型管理" , description = "修改车型管理" )
    @SysLog("修改车型管理")
    @PutMapping
    public R updateById(@RequestBody CqCarModelEntity cqCarModel) {
        ApiAsserts.isTrue(cqCarModel.getId() != null, "id不能为空");
        return R.ok(cqCarModelService.updateById(cqCarModel));
    }

    /**
     * 通过id删除车型管理
     *
     * @param ids id列表
     * @return R
     */
    // @Operation(summary = "通过id删除车型管理" , description = "通过id删除车型管理" )
    @SysLog("通过id删除车型管理")
    @DeleteMapping
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(cqCarModelService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param cqCarModel 查询条件
     * @param ids        导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<CqCarModelEntity> export(CqCarModelEntity cqCarModel, Integer[] ids) {
        return cqCarModelService.list(Wrappers.lambdaQuery(cqCarModel).in(ArrayUtil.isNotEmpty(ids), CqCarModelEntity::getId, ids));
    }
}
